package com.dalor.phonesmanager.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.dalor.phonesmanager.R;
import com.dalor.phonesmanager.bean.Folder;

import java.io.File;
import java.util.ArrayList;

/**
 * 作者： 吴昶 .
 * 时间: 2018/12/28 14:46
 * 功能简介：
 */
public class GridPhoneAdapter extends BaseAdapter {

    private Context context;
    private ArrayList<Folder> data;

    public GridPhoneAdapter(Context context){
        this.context=context;
    }

    public void setData(ArrayList<Folder> data) {
        this.data = data;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return data==null?0:data.size();
    }

    @Override
    public Object getItem(int i) {
        return data.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        Holder holder=null;
        if(view==null){
            holder=new Holder();
            view=LayoutInflater.from(context).inflate(R.layout.layout_phone_item,null);
            holder.imageView=(ImageView)view.findViewById(R.id.iv_first_image);
            holder.textView=(TextView)view.findViewById(R.id.tv_folder_name);
            view.setTag(holder);
        }else {
            holder=(Holder)view.getTag();
        }
        Glide.with(context).load(new File(data.get(i).images.get(0).path)).into(holder.imageView);
        holder.textView.setText(data.get(i).name+"("+data.get(i).images.size()+")");
        return view;
    }

    private class Holder{
        private ImageView imageView;
        private TextView textView;
    }

}
